home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / patch / pch.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  33KB  |  1,300 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6. /* $Header: /home/dice/com/src/patch/RCS/pch.c,v 30.8 1994/08/18 05:51:57 dice Exp dice $
  7.  *
  8.  * $Log: pch.c,v $
  9.  * Revision 30.8  1994/08/18  05:51:57  dice
  10.  * .
  11.  *
  12.  * Revision 30.0  1994/06/10  18:08:51  dice
  13.  * .
  14.  *
  15.  * Revision 30.0  1994/06/10  18:08:51  dice
  16.  * .
  17.  *
  18.  * Revision 2.0.2.0  90/05/01  22:17:51  davison
  19.  * patch12u: unidiff support added
  20.  *
  21.  * Revision 2.0.1.7  88/06/03  15:13:28  lwall
  22.  * patch10: Can now find patches in shar scripts.
  23.  * patch10: Hunks that swapped and then swapped back could core dump.
  24.  *
  25.  * Revision 2.0.1.6  87/06/04  16:18:13  lwall
  26.  * pch_swap didn't swap p_bfake and p_efake.
  27.  *
  28.  * Revision 2.0.1.5  87/01/30  22:47:42  lwall
  29.  * Improved responses to mangled patches.
  30.  *
  31.  * Revision 2.0.1.4  87/01/05  16:59:53  lwall
  32.  * New-style context diffs caused double call to free().
  33.  *
  34.  * Revision 2.0.1.3  86/11/14  10:08:33  lwall
  35.  * Fixed problem where a long pattern wouldn't grow the hunk.
  36.  * Also restored p_input_line when backtracking so error messages are right.
  37.  *
  38.  * Revision 2.0.1.2  86/11/03  17:49:52  lwall
  39.  * New-style delete triggers spurious assertion error.
  40.  *
  41.  * Revision 2.0.1.1  86/10/29  15:52:08  lwall
  42.  * Could falsely report new-style context diff.
  43.  *
  44.  * Revision 2.0  86/09/17  15:39:37  lwall
  45.  * Baseline for netwide release.
  46.  *
  47.  */
  48.  
  49. #include "EXTERN.h"
  50. #include "common.h"
  51. #include "util.h"
  52. #include "INTERN.h"
  53. #include "pch.h"
  54.  
  55. /* Patch (diff listing) abstract type. */
  56.  
  57. static long p_filesize;         /* size of the patch file */
  58. static LINENUM p_first;         /* 1st line number */
  59. static LINENUM p_newfirst;        /* 1st line number of replacement */
  60. static LINENUM p_ptrn_lines;        /* # lines in pattern */
  61. static LINENUM p_repl_lines;        /* # lines in replacement text */
  62. static LINENUM p_end = -1;        /* last line in hunk */
  63. static LINENUM p_max;            /* max allowed value of p_end */
  64. static LINENUM p_context = 3;        /* # of context lines */
  65. static LINENUM p_input_line = 0;    /* current line # from patch file */
  66. static char **p_line = Null(char**);    /* the text of the hunk */
  67. static short *p_len = Null(short*);     /* length of each line */
  68. static char *p_char = Nullch;        /* +, -, and ! */
  69. static int hunkmax = INITHUNKMAX;    /* size of above arrays to begin with */
  70. static int p_indent;            /* indent to patch */
  71. static LINENUM p_base;            /* where to intuit this time */
  72. static LINENUM p_bline;         /* line # of p_base */
  73. static LINENUM p_start;         /* where intuit found a patch */
  74. static LINENUM p_sline;         /* and the line number for it */
  75. static LINENUM p_hunk_beg;        /* line number of current hunk */
  76. static LINENUM p_efake = -1;        /* end of faked up lines--don't free */
  77. static LINENUM p_bfake = -1;        /* beg of faked up lines */
  78.  
  79. /* Prepare to look for the next patch in the patch file. */
  80.  
  81. void
  82. re_patch()
  83. {
  84.     p_first = Nulline;
  85.     p_newfirst = Nulline;
  86.     p_ptrn_lines = Nulline;
  87.     p_repl_lines = Nulline;
  88.     p_end = (LINENUM)-1;
  89.     p_max = Nulline;
  90.     p_indent = 0;
  91. }
  92.  
  93. /* Open the patch file at the beginning of time. */
  94.  
  95. void
  96. open_patch_file(filename)
  97. char *filename;
  98. {
  99.     if (filename == Nullch || !*filename || strEQ(filename, "-")) {
  100.     pfp = fopen(TMPPATNAME, "w");
  101.     if (pfp == Nullfp)
  102.         fatal2("patch: can't create %s.\n", TMPPATNAME);
  103.     while (fgets(buf, sizeof buf, stdin) != Nullch)
  104.         fputs(buf, pfp);
  105.     Fclose(pfp);
  106.     filename = TMPPATNAME;
  107.     }
  108.     pfp = fopen(filename, "r");
  109.     if (pfp == Nullfp)
  110.     fatal2("patch file %s not found\n", filename);
  111.     Fstat(fileno(pfp), &filestat);
  112.     p_filesize = filestat.st_size;
  113.     next_intuit_at(0L,1L);                      /* start at the beginning */
  114.     set_hunkmax();
  115. }
  116.  
  117. /* Make sure our dynamically realloced tables are malloced to begin with. */
  118.  
  119. void
  120. set_hunkmax()
  121. {
  122. #ifndef lint
  123.     if (p_line == Null(char**))
  124.     p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
  125.     if (p_len == Null(short*))
  126.     p_len  = (short*) malloc((MEM)hunkmax * sizeof(short));
  127. #endif
  128.     if (p_char == Nullch)
  129.     p_char = (char*)  malloc((MEM)hunkmax * sizeof(char));
  130. }
  131.  
  132. /* Enlarge the arrays containing the current hunk of patch. */
  133.  
  134. void
  135. grow_hunkmax()
  136. {
  137.     hunkmax *= 2;
  138.     /*
  139.      * Note that on most systems, only the p_line array ever gets fresh memory
  140.      * since p_len can move into p_line's old space, and p_char can move into
  141.      * p_len's old space.  Not on PDP-11's however.  But it doesn't matter.
  142.      */
  143.     assert(p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch);
  144. #ifndef lint
  145.     p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
  146.     p_len  = (short*) realloc((char*)p_len,  (MEM)hunkmax * sizeof(short));
  147.     p_char = (char*)  realloc((char*)p_char, (MEM)hunkmax * sizeof(char));
  148. #endif
  149.     if (p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch)
  150.     return;
  151.     if (!using_plan_a)
  152.     fatal1("patch: out of memory (grow_hunkmax)\n");
  153.     out_of_mem = TRUE;        /* whatever is null will be allocated again */
  154.                 /* from within plan_a(), of all places */
  155. }
  156.  
  157. /* True if the remainder of the patch file contains a diff of some sort. */
  158.  
  159. bool
  160. there_is_another_patch()
  161. {
  162.     if (p_base != 0L && p_base >= p_filesize) {
  163.     if (verbose)
  164.         say1("done\n");
  165.     return FALSE;
  166.     }
  167.     if (verbose)
  168.     say1("Hmm...");
  169.     diff_type = intuit_diff_type();
  170.     if (!diff_type) {
  171.     if (p_base != 0L) {
  172.         if (verbose)
  173.         say1("  Ignoring the trailing garbage.\ndone\n");
  174.     }
  175.     else
  176.         say1("  I can't seem to find a patch in there anywhere.\n");
  177.     return FALSE;
  178.     }
  179.     if (verbose)
  180.     say3("  %sooks like %s to me...\n",
  181.         (p_base == 0L ? "L" : "The next patch l"),
  182.         diff_type == UNI_DIFF ? "a unified diff" :
  183.         diff_type == CONTEXT_DIFF ? "a context diff" :
  184.         diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
  185.         diff_type == NORMAL_DIFF ? "a normal diff" :
  186.         "an ed script" );
  187.     if (p_indent && verbose)
  188.     say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
  189.     skip_to(p_start,p_sline);
  190.     while (filearg[0] == Nullch) {
  191.     if (force) {
  192.         say1("No file to patch.  Skipping...\n");
  193.         filearg[0] = savestr(bestguess);
  194.         return TRUE;
  195.     }
  196.     ask1("File to patch: ");
  197.     if (*buf != '\n') {
  198.         if (bestguess)
  199.         free(bestguess);
  200.         bestguess = savestr(buf);
  201.         filearg[0] = fetchname(buf, 0, FALSE);
  202.     }
  203.     if (filearg[0] == Nullch) {
  204.         ask1("No file found--skip this patch? [n] ");
  205.         if (*buf != 'y') {
  206.         continue;
  207.         }
  208.         if (verbose)
  209.         say1("Skipping patch...\n");
  210.         filearg[0] = fetchname(bestguess, 0, TRUE);
  211.         skip_rest_of_patch = TRUE;
  212.         return TRUE;
  213.     }
  214.     }
  215.     return TRUE;
  216. }
  217.  
  218. /* Determine what kind of diff is in the remaining part of the patch file. */
  219.  
  220. int
  221. intuit_diff_type()
  222. {
  223.     Reg4 long this_line = 0;
  224.     Reg5 long previous_line;
  225.     Reg6 long first_command_line = -1;
  226.     long fcl_line;
  227.     Reg7 bool last_line_was_command = FALSE;
  228.     Reg8 bool this_is_a_command = FALSE;
  229.     Reg9 bool stars_last_line = FALSE;
  230.     Reg10 bool stars_this_line = FALSE;
  231.     Reg3 int indent;
  232.     Reg1 char *s;
  233.     Reg2 char *t;
  234.     char *indtmp = Nullch;
  235.     char *oldtmp = Nullch;
  236.     char *newtmp = Nullch;
  237.     char *indname = Nullch;
  238.     char *oldname = Nullch;
  239.     char *newname = Nullch;
  240.     Reg11 int retval;
  241.     bool no_filearg = (filearg[0] == Nullch);
  242.  
  243.     ok_to_create_file = FALSE;
  244.     Fseek(pfp, p_base, 0);
  245.     p_input_line = p_bline - 1;
  246.     for (;;) {
  247.     previous_line = this_line;
  248.     last_line_was_command = this_is_a_command;
  249.     stars_last_line = stars_this_line;
  250.     this_line = ftell(pfp);
  251.     indent = 0;
  252.     p_input_line++;
  253.     if (fgets(buf, sizeof buf, pfp) == Nullch) {
  254.         if (first_command_line >= 0L) {
  255.                     /* nothing but deletes!? */
  256.         p_start = first_command_line;
  257.         p_sline = fcl_line;
  258.         retval = ED_DIFF;
  259.         goto scan_exit;
  260.         }
  261.         else {
  262.         p_start = this_line;
  263.         p_sline = p_input_line;
  264.         retval = 0;
  265.         goto scan_exit;
  266.         }
  267.     }
  268.     for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
  269.         if (*s == '\t')
  270.         indent += 8 - (indent % 8);
  271.         else
  272.         indent++;
  273.     }
  274.     for (t=s; isdigit(*t) || *t == ','; t++) ;
  275.     this_is_a_command = (isdigit(*s) &&
  276.       (*t == 'd' || *t == 'c' || *t == 'a') );
  277.     if (first_command_line < 0L && this_is_a_command) {
  278.         first_command_line = this_line;
  279.         fcl_line = p_input_line;
  280.         p_indent = indent;        /* assume this for now */
  281.     }
  282.     if (!stars_last_line && strnEQ(s, "*** ", 4))
  283.         oldtmp = savestr(s+4);
  284.     else if (strnEQ(s, "--- ", 4))
  285.         newtmp = savestr(s+4);
  286.     else if (strnEQ(s, "+++ ", 4))
  287.         oldtmp = savestr(s+4);      /* pretend it is the old name */
  288.     else if (strnEQ(s, "Index:", 6))
  289.         indtmp = savestr(s+6);
  290.     else if (strnEQ(s, "Prereq:", 7)) {
  291.         for (t=s+7; isspace(*t); t++) ;
  292.         revision = savestr(t);
  293.         for (t=revision; *t && !isspace(*t); t++) ;
  294.         *t = '\0';
  295.         if (!*revision) {
  296.         free(revision);
  297.         revision = Nullch;
  298.         }
  299.     }
  300.     if ((!diff_type || diff_type == ED_DIFF) &&
  301.       first_command_line >= 0L &&
  302.       strEQ(s, ".\n") ) {
  303.         p_indent = indent;
  304.         p_start = first_command_line;
  305.         p_sline = fcl_line;
  306.         retval = ED_DIFF;
  307.         goto scan_exit;
  308.     }
  309.     if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) {
  310.         if (!atol(s+3))
  311.         ok_to_create_file = TRUE;
  312.         p_indent = indent;
  313.         p_start = this_line;
  314.         p_sline = p_input_line;
  315.         retval = UNI_DIFF;
  316.         goto scan_exit;
  317.     }
  318.     stars_this_line = strnEQ(s, "********", 8);
  319.     if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
  320.          strnEQ(s, "*** ", 4)) {
  321.         if (!atol(s+4))
  322.         ok_to_create_file = TRUE;
  323.         /* if this is a new context diff the character just before */
  324.         /* the newline is a '*'. */
  325.         while (*s != '\n')
  326.         s++;
  327.         p_indent = indent;
  328.         p_start = previous_line;
  329.         p_sline = p_input_line - 1;
  330.         retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
  331.         goto scan_exit;
  332.     }
  333.     if ((!diff_type || diff_type == NORMAL_DIFF) &&
  334.       last_line_was_command &&
  335.       (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
  336.         p_start = previous_line;
  337.         p_sline = p_input_line - 1;
  338.         p_indent = indent;
  339.         retval = NORMAL_DIFF;
  340.         goto scan_exit;
  341.     }
  342.     }
  343.   scan_exit:
  344.     if (no_filearg) {
  345.     if (indtmp != Nullch)
  346.         indname = fetchname(indtmp, strippath, ok_to_create_file);
  347.     if (oldtmp != Nullch)
  348.         oldname = fetchname(oldtmp, strippath, ok_to_create_file);
  349.     if (newtmp != Nullch)
  350.         newname = fetchname(newtmp, strippath, ok_to_create_file);
  351.     if (oldname && newname) {
  352.         if (strlen(oldname) < strlen(newname))
  353.         filearg[0] = savestr(oldname);
  354.         else
  355.         filearg[0] = savestr(newname);
  356.     }
  357.     else if (oldname)
  358.         filearg[0] = savestr(oldname);
  359.     else if (newname)
  360.         filearg[0] = savestr(newname);
  361.     else if (indname)
  362.         filearg[0] = savestr(indname);
  363.     }
  364.     if (bestguess) {
  365.     free(bestguess);
  366.     bestguess = Nullch;
  367.     }
  368.     if (filearg[0] != Nullch)
  369.     bestguess = savestr(filearg[0]);
  370.     else if (indtmp != Nullch)
  371.     bestguess = fetchname(indtmp, strippath, TRUE);
  372.     else {
  373.     if (oldtmp != Nullch)
  374.         oldname = fetchname(oldtmp, strippath, TRUE);
  375.     if (newtmp != Nullch)
  376.         newname = fetchname(newtmp, strippath, TRUE);
  377.     if (oldname && newname) {
  378.         if (strlen(oldname) < strlen(newname))
  379.         bestguess = savestr(oldname);
  380.         else
  381.         bestguess = savestr(newname);
  382.     }
  383.     else if (oldname)
  384.         bestguess = savestr(oldname);
  385.     else if (newname)
  386.         bestguess = savestr(newname);
  387.     }
  388.     if (indtmp != Nullch)
  389.     free(indtmp);
  390.     if (oldtmp != Nullch)
  391.     free(oldtmp);
  392.     if (newtmp != Nullch)
  393.     free(newtmp);
  394.     if (indname != Nullch)
  395.     free(indname);
  396.     if (oldname != Nullch)
  397.     free(oldname);
  398.     if (newname != Nullch)
  399.     free(newname);
  400.     return retval;
  401. }
  402.  
  403. /* Remember where this patch ends so we know where to start up again. */
  404.  
  405. void
  406. next_intuit_at(file_pos,file_line)
  407. long file_pos;
  408. long file_line;
  409. {
  410.     p_base = file_pos;
  411.     p_bline = file_line;
  412. }
  413.  
  414. /* Basically a verbose fseek() to the actual diff listing. */
  415.  
  416. void
  417. skip_to(file_pos,file_line)
  418. long file_pos;
  419. long file_line;
  420. {
  421.     char *ret;
  422.  
  423.     assert(p_base <= file_pos);
  424.     if (verbose && p_base < file_pos) {
  425.     Fseek(pfp, p_base, 0);
  426.     say1("The text leading up to this was:\n--------------------------\n");
  427.     while (ftell(pfp) < file_pos) {
  428.         ret = fgets(buf, sizeof buf, pfp);
  429.         assert(ret != Nullch);
  430.         say2("|%s", buf);
  431.     }
  432.     say1("--------------------------\n");
  433.     }
  434.     else
  435.     Fseek(pfp, file_pos, 0);
  436.     p_input_line = file_line - 1;
  437. }
  438.  
  439. /* Make this a function for better debugging.  */
  440. static void
  441. malformed ()
  442. {
  443.     fatal3("Malformed patch at line %ld: %s", p_input_line, buf);
  444.         /* about as informative as "Syntax error" in C */
  445. }
  446.  
  447. /* True if there is more of the current diff listing to process. */
  448.  
  449. bool
  450. another_hunk()
  451. {
  452.     Reg1 char *s;
  453.     Reg8 char *ret;
  454.     Reg2 int context = 0;
  455.  
  456.     while (p_end >= 0) {
  457.     if (p_end == p_efake)
  458.         p_end = p_bfake;        /* don't free twice */
  459.     else
  460.         free(p_line[p_end]);
  461.     p_end--;
  462.     }
  463.     assert(p_end == -1);
  464.     p_efake = -1;
  465.  
  466.     p_max = hunkmax;            /* gets reduced when --- found */
  467.     if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
  468.     long line_beginning = ftell(pfp);
  469.                     /* file pos of the current line */
  470.     LINENUM repl_beginning = 0;    /* index of --- line */
  471.     Reg4 LINENUM fillcnt = 0;    /* #lines of missing ptrn or repl */
  472.     Reg5 LINENUM fillsrc;        /* index of first line to copy */
  473.     Reg6 LINENUM filldst;        /* index of first missing line */
  474.     bool ptrn_spaces_eaten = FALSE; /* ptrn was slightly misformed */
  475.     Reg9 bool repl_could_be_missing = TRUE;
  476.                     /* no + or ! lines in this hunk */
  477.     bool repl_missing = FALSE;    /* we are now backtracking */
  478.     long repl_backtrack_position = 0;
  479.                     /* file pos of first repl line */
  480.     LINENUM repl_patch_line;    /* input line number for same */
  481.     Reg7 LINENUM ptrn_copiable = 0;
  482.                     /* # of copiable lines in ptrn */
  483.  
  484.     ret = pgets(buf, sizeof buf, pfp);
  485.     p_input_line++;
  486.     if (ret == Nullch || strnNE(buf, "********", 8)) {
  487.         next_intuit_at(line_beginning,p_input_line);
  488.         return FALSE;
  489.     }
  490.     p_context = 100;
  491.     p_hunk_beg = p_input_line + 1;
  492.     while (p_end < p_max) {
  493.         line_beginning = ftell(pfp);
  494.         ret = pgets(buf, sizeof buf, pfp);
  495.         p_input_line++;
  496.         if (ret == Nullch) {
  497.         if (p_max - p_end < 4)
  498.             Strcpy(buf, "  \n");  /* assume blank lines got chopped */
  499.         else {
  500.             if (repl_beginning && repl_could_be_missing) {
  501.             repl_missing = TRUE;
  502.             goto hunk_done;
  503.             }
  504.             fatal1("Unexpected end of file in patch.\n");
  505.         }
  506.         }
  507.         p_end++;
  508.         assert(p_end < hunkmax);
  509.         p_char[p_end] = *buf;
  510. #ifdef zilog
  511.         p_line[(short)p_end] = Nullch;
  512. #else
  513.         p_line[p_end] = Nullch;
  514. #endif
  515.         switch (*buf) {
  516.         case '*':
  517.         if (strnEQ(buf, "********", 8)) {
  518.             if (repl_beginning && repl_could_be_missing) {
  519.             repl_missing = TRUE;
  520.             goto hunk_done;
  521.             }
  522.             else
  523.             fatal2("Unexpected end of hunk at line %ld.\n",
  524.                 p_input_line);
  525.         }
  526.         if (p_end != 0) {
  527.             if (repl_beginning && repl_could_be_missing) {
  528.             repl_missing = TRUE;
  529.             goto hunk_done;
  530.             }
  531.             fatal3("Unexpected *** at line %ld: %s", p_input_line, buf);
  532.         }
  533.         context = 0;
  534.         p_line[p_end] = savestr(buf);
  535.         if (out_of_mem) {
  536.             p_end--;
  537.             return FALSE;
  538.         }
  539.         for (s=buf; *s && !isdigit(*s); s++) ;
  540.         if (!*s)
  541.             malformed ();
  542.         if (strnEQ(s,"0,0",3))
  543.             strcpy(s,s+2);
  544.         p_first = (LINENUM) atol(s);
  545.         while (isdigit(*s)) s++;
  546.         if (*s == ',') {
  547.             for (; *s && !isdigit(*s); s++) ;
  548.             if (!*s)
  549.             malformed ();
  550.             p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
  551.         }
  552.         else if (p_first)
  553.             p_ptrn_lines = 1;
  554.         else {
  555.             p_ptrn_lines = 0;
  556.             p_first = 1;
  557.         }
  558.         p_max = p_ptrn_lines + 6;    /* we need this much at least */
  559.         while (p_max >= hunkmax)
  560.             grow_hunkmax();
  561.         p_max = hunkmax;
  562.         break;
  563.         case '-':
  564.         if (buf[1] == '-') {
  565.             if (repl_beginning ||
  566.             (p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
  567.             {
  568.             if (p_end == 1) {
  569.                 /* `old' lines were omitted - set up to fill */
  570.                 /* them in from 'new' context lines. */
  571.                 p_end = p_ptrn_lines + 1;
  572.                 fillsrc = p_end + 1;
  573.                 filldst = 1;
  574.                 fillcnt = p_ptrn_lines;
  575.             }
  576.             else {
  577.                 if (repl_beginning) {
  578.                 if (repl_could_be_missing){
  579.                     repl_missing = TRUE;
  580.                     goto hunk_done;
  581.                 }
  582.                 fatal3(
  583. "Duplicate \"---\" at line %ld--check line numbers at line %ld.\n",
  584.                     p_input_line, p_hunk_beg + repl_beginning);
  585.                 }
  586.                 else {
  587.                 fatal4(
  588. "%s \"---\" at line %ld--check line numbers at line %ld.\n",
  589.                     (p_end <= p_ptrn_lines
  590.                     ? "Premature"
  591.                     : "Overdue" ),
  592.                     p_input_line, p_hunk_beg);
  593.                 }
  594.             }
  595.             }
  596.             repl_beginning = p_end;
  597.             repl_backtrack_position = ftell(pfp);
  598.             repl_patch_line = p_input_line;
  599.             p_line[p_end] = savestr(buf);
  600.             if (out_of_mem) {
  601.             p_end--;
  602.             return FALSE;
  603.             }
  604.             p_char[p_end] = '=';
  605.             for (s=buf; *s && !isdigit(*s); s++) ;
  606.             if (!*s)
  607.             malformed ();
  608.             p_newfirst = (LINENUM) atol(s);
  609.             while (isdigit(*s)) s++;
  610.             if (*s == ',') {
  611.             for (; *s && !isdigit(*s); s++) ;
  612.             if (!*s)
  613.                 malformed ();
  614.             p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
  615.             }
  616.             else if (p_newfirst)
  617.             p_repl_lines = 1;
  618.             else {
  619.             p_repl_lines = 0;
  620.             p_newfirst = 1;
  621.             }
  622.             p_max = p_repl_lines + p_end;
  623.             if (p_max > MAXHUNKSIZE)
  624.             fatal4("Hunk too large (%ld lines) at line %ld: %s",
  625.                   p_max, p_input_line, buf);
  626.             while (p_max >= hunkmax)
  627.             grow_hunkmax();
  628.             if (p_repl_lines != ptrn_copiable)
  629.             repl_could_be_missing = FALSE;
  630.             break;
  631.         }
  632.         goto change_line;
  633.         case '+':  case '!':
  634.         repl_could_be_missing = FALSE;
  635.           change_line:
  636.         if (buf[1] == '\n' && canonicalize)
  637.             strcpy(buf+1," \n");
  638.         if (!isspace(buf[1]) && buf[1] != '>' && buf[1] != '<' &&
  639.           repl_beginning && repl_could_be_missing) {
  640.             repl_missing = TRUE;
  641.             goto hunk_done;
  642.         }
  643.         if (context > 0) {
  644.             if (context < p_context)
  645.             p_context = context;
  646.             context = -1000;
  647.         }
  648.         p_line[p_end] = savestr(buf+2);
  649.         if (out_of_mem) {
  650.             p_end--;
  651.             return FALSE;
  652.         }
  653.         break;
  654.         case '\t': case '\n':       /* assume the 2 spaces got eaten */
  655.         if (repl_beginning && repl_could_be_missing &&
  656.           (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
  657.             repl_missing = TRUE;
  658.             goto hunk_done;
  659.         }
  660.         p_line[p_end] = savestr(buf);
  661.         if (out_of_mem) {
  662.             p_end--;
  663.             return FALSE;
  664.         }
  665.         if (p_end != p_ptrn_lines + 1) {
  666.             ptrn_spaces_eaten |= (repl_beginning != 0);
  667.             context++;
  668.             if (!repl_beginning)
  669.             ptrn_copiable++;
  670.             p_char[p_end] = ' ';
  671.         }
  672.         break;
  673.         case ' ':
  674.         if (!isspace(buf[1]) &&
  675.           repl_beginning && repl_could_be_missing) {
  676.             repl_missing = TRUE;
  677.             goto hunk_done;
  678.         }
  679.         context++;
  680.         if (!repl_beginning)
  681.             ptrn_copiable++;
  682.         p_line[p_end] = savestr(buf+2);
  683.         if (out_of_mem) {
  684.             p_end--;
  685.             return FALSE;
  686.         }
  687.         break;
  688.         default:
  689.         if (repl_beginning && repl_could_be_missing) {
  690.             repl_missing = TRUE;
  691.             goto hunk_done;
  692.         }
  693.         malformed ();
  694.         }
  695.         /* set up p_len for strncmp() so we don't have to */
  696.         /* assume null termination */
  697.         if (p_line[p_end])
  698.         p_len[p_end] = strlen(p_line[p_end]);
  699.         else
  700.         p_len[p_end] = 0;
  701.     }
  702.  
  703.     hunk_done:
  704.     if (p_end >=0 && !repl_beginning)
  705.         fatal2("No --- found in patch at line %ld\n", pch_hunk_beg());
  706.  
  707.     if (repl_missing) {
  708.  
  709.         /* reset state back to just after --- */
  710.         p_input_line = repl_patch_line;
  711.         for (p_end--; p_end > repl_beginning; p_end--)
  712.         free(p_line[p_end]);
  713.         Fseek(pfp, repl_backtrack_position, 0);
  714.  
  715.         /* redundant 'new' context lines were omitted - set */
  716.         /* up to fill them in from the old file context */
  717.         fillsrc = 1;
  718.         filldst = repl_beginning+1;
  719.         fillcnt = p_repl_lines;
  720.         p_end = p_max;
  721.     }
  722.  
  723.     if (diff_type == CONTEXT_DIFF &&
  724.       (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
  725.         if (verbose)
  726.         say4("%s\n%s\n%s\n",
  727. "(Fascinating--this is really a new-style context diff but without",
  728. "the telltale extra asterisks on the *** line that usually indicate",
  729. "the new style...)");
  730.         diff_type = NEW_CONTEXT_DIFF;
  731.     }
  732.  
  733.     /* if there were omitted context lines, fill them in now */
  734.     if (fillcnt) {
  735.         p_bfake = filldst;        /* remember where not to free() */
  736.         p_efake = filldst + fillcnt - 1;
  737.         while (fillcnt-- > 0) {
  738.         while (fillsrc <= p_end && p_char[fillsrc] != ' ')
  739.             fillsrc++;
  740.         if (fillsrc > p_end)
  741.             fatal2("Replacement text or line numbers mangled in hunk at line %ld\n",
  742.             p_hunk_beg);
  743.         p_line[filldst] = p_line[fillsrc];
  744.         p_char[filldst] = p_char[fillsrc];
  745.         p_len[filldst] = p_len[fillsrc];
  746.         fillsrc++; filldst++;
  747.         }
  748.         while (fillsrc <= p_end && fillsrc != repl_beginning &&
  749.           p_char[fillsrc] != ' ')
  750.         fillsrc++;
  751. #ifdef DEBUGGING
  752.         if (debug & 64)
  753.         printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
  754.             fillsrc,filldst,repl_beginning,p_end+1);
  755. #endif
  756.         assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
  757.         assert(filldst==p_end+1 || filldst==repl_beginning);
  758.     }
  759.     }
  760.     else if (diff_type == UNI_DIFF) {
  761.     long line_beginning = ftell(pfp);
  762.                     /* file pos of the current line */
  763.     Reg4 LINENUM fillsrc;        /* index of old lines */
  764.     Reg5 LINENUM filldst;        /* index of new lines */
  765.     char ch;
  766.  
  767.     ret = pgets(buf, sizeof buf, pfp);
  768.     p_input_line++;
  769.     if (ret == Nullch || strnNE(buf, "@@ -", 4)) {
  770.         next_intuit_at(line_beginning,p_input_line);
  771.         return FALSE;
  772.     }
  773.     s = buf+4;
  774.     if (!*s)
  775.         malformed ();
  776.     p_first = (LINENUM) atol(s);
  777.     while (isdigit(*s)) s++;
  778.     if (*s == ',') {
  779.         p_ptrn_lines = (LINENUM) atol(++s);
  780.         while (isdigit(*s)) s++;
  781.     } else
  782.         p_ptrn_lines = 1;
  783.     if (*s == ' ') s++;
  784.     if (*s != '+' || !*++s)
  785.         malformed ();
  786.     p_newfirst = (LINENUM) atol(s);
  787.     while (isdigit(*s)) s++;
  788.     if (*s == ',') {
  789.         p_repl_lines = (LINENUM) atol(++s);
  790.         while (isdigit(*s)) s++;
  791.     } else
  792.         p_repl_lines = 1;
  793.     if (*s == ' ') s++;
  794.     if (*s != '@')
  795.         malformed ();
  796.     if (!p_first && !p_ptrn_lines)
  797.         p_first = 1;
  798.     p_max = p_ptrn_lines + p_repl_lines;
  799.     while (p_max >= hunkmax)
  800.         grow_hunkmax();
  801.     p_max = hunkmax;
  802.     fillsrc = 1;
  803.     filldst = fillsrc + p_ptrn_lines;
  804.     p_end = filldst + p_repl_lines;
  805.     Sprintf(buf,"*** %ld,%ld ****\n",p_first,p_first + p_ptrn_lines - 1);
  806.     p_line[0] = savestr(buf);
  807.     if (out_of_mem) {
  808.         p_end = -1;
  809.         return FALSE;
  810.     }
  811.     p_char[0] = '*';
  812.     Sprintf(buf,"--- %ld,%ld ----\n",p_newfirst,p_newfirst+p_repl_lines-1);
  813.     p_line[filldst] = savestr(buf);
  814.     if (out_of_mem) {
  815.         p_end = 0;
  816.         return FALSE;
  817.     }
  818.     p_char[filldst++] = '=';
  819.     p_context = 100;
  820.     context = 0;
  821.     p_hunk_beg = p_input_line + 1;
  822.     while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
  823.         line_beginning = ftell(pfp);
  824.         ret = pgets(buf, sizeof buf, pfp);
  825.         p_input_line++;
  826.         if (ret == Nullch) {
  827.         if (p_max - filldst < 3)
  828.             Strcpy(buf, " \n");  /* assume blank lines got chopped */
  829.         else {
  830.             fatal1("Unexpected end of file in patch.\n");
  831.         }
  832.         }
  833.         if (*buf == '\t' || *buf == '\n') {
  834.         ch = ' ';               /* assume the space got eaten */
  835.         s = savestr(buf);
  836.         }
  837.         else {
  838.         ch = *buf;
  839.         s = savestr(buf+1);
  840.         }
  841.         if (out_of_mem) {
  842.         while (--filldst > p_ptrn_lines)
  843.             free(p_line[filldst]);
  844.         p_end = fillsrc-1;
  845.         return FALSE;
  846.         }
  847.         switch (ch) {
  848.         case '-':
  849.         if (fillsrc > p_ptrn_lines) {
  850.             free(s);
  851.             p_end = filldst-1;
  852.             malformed ();
  853.         }
  854.         p_char[fillsrc] = ch;
  855.         p_line[fillsrc] = s;
  856.         p_len[fillsrc++] = strlen(s);
  857.         break;
  858.         case '=':
  859.         ch = ' ';
  860.         /* FALL THROUGH */
  861.         case ' ':
  862.         if (fillsrc > p_ptrn_lines) {
  863.             free(s);
  864.             while (--filldst > p_ptrn_lines)
  865.             free(p_line[filldst]);
  866.             p_end = fillsrc-1;
  867.             malformed ();
  868.         }
  869.         context++;
  870.         p_char[fillsrc] = ch;
  871.         p_line[fillsrc] = s;
  872.         p_len[fillsrc++] = strlen(s);
  873.         s = savestr(s);
  874.         if (out_of_mem) {
  875.             while (--filldst > p_ptrn_lines)
  876.             free(p_line[filldst]);
  877.             p_end = fillsrc-1;
  878.             return FALSE;
  879.         }
  880.         /* FALL THROUGH */
  881.         case '+':
  882.         if (filldst > p_end) {
  883.             free(s);
  884.             while (--filldst > p_ptrn_lines)
  885.             free(p_line[filldst]);
  886.             p_end = fillsrc-1;
  887.             malformed ();
  888.         }
  889.         p_char[filldst] = ch;
  890.         p_line[filldst] = s;
  891.         p_len[filldst++] = strlen(s);
  892.         break;
  893.         default:
  894.         p_end = filldst;
  895.         malformed ();
  896.         }
  897.         if (ch != ' ' && context > 0) {
  898.         if (context < p_context)
  899.             p_context = context;
  900.         context = -1000;
  901.         }
  902.     }/* while */
  903.     }
  904.     else {                /* normal diff--fake it up */
  905.     char hunk_type;
  906.     Reg3 int i;
  907.     LINENUM min, max;
  908.     long line_beginning = ftell(pfp);
  909.  
  910.     p_context = 0;
  911.     ret = pgets(buf, sizeof buf, pfp);
  912.     p_input_line++;
  913.     if (ret == Nullch || !isdigit(*buf)) {
  914.         next_intuit_at(line_beginning,p_input_line);
  915.         return FALSE;
  916.     }
  917.     p_first = (LINENUM)atol(buf);
  918.     for (s=buf; isdigit(*s); s++) ;
  919.     if (*s == ',') {
  920.         p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
  921.         while (isdigit(*s)) s++;
  922.     }
  923.     else
  924.         p_ptrn_lines = (*s != 'a');
  925.     hunk_type = *s;
  926.     if (hunk_type == 'a')
  927.         p_first++;            /* do append rather than insert */
  928.     min = (LINENUM)atol(++s);
  929.     for (; isdigit(*s); s++) ;
  930.     if (*s == ',')
  931.         max = (LINENUM)atol(++s);
  932.     else
  933.         max = min;
  934.     if (hunk_type == 'd')
  935.         min++;
  936.     p_end = p_ptrn_lines + 1 + max - min + 1;
  937.     if (p_end > MAXHUNKSIZE)
  938.         fatal4("Hunk too large (%ld lines) at line %ld: %s",
  939.           p_end, p_input_line, buf);
  940.     while (p_end >= hunkmax)
  941.         grow_hunkmax();
  942.     p_newfirst = min;
  943.     p_repl_lines = max - min + 1;
  944.     Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
  945.     p_line[0] = savestr(buf);
  946.     if (out_of_mem) {
  947.         p_end = -1;
  948.         return FALSE;
  949.     }
  950.     p_char[0] = '*';
  951.     for (i=1; i<=p_ptrn_lines; i++) {
  952.         ret = pgets(buf, sizeof buf, pfp);
  953.         p_input_line++;
  954.         if (ret == Nullch)
  955.         fatal2("Unexpected end of file in patch at line %ld.\n",
  956.           p_input_line);
  957.         if (*buf != '<')
  958.         fatal2("< expected at line %ld of patch.\n", p_input_line);
  959.         p_line[i] = savestr(buf+2);
  960.         if (out_of_mem) {
  961.         p_end = i-1;
  962.         return FALSE;
  963.         }
  964.         p_len[i] = strlen(p_line[i]);
  965.         p_char[i] = '-';
  966.     }
  967.     if (hunk_type == 'c') {
  968.         ret = pgets(buf, sizeof buf, pfp);
  969.         p_input_line++;
  970.         if (ret == Nullch)
  971.         fatal2("Unexpected end of file in patch at line %ld.\n",
  972.             p_input_line);
  973.         if (*buf != '-')
  974.         fatal2("--- expected at line %ld of patch.\n", p_input_line);
  975.     }
  976.     Sprintf(buf, "--- %ld,%ld\n", min, max);
  977.     p_line[i] = savestr(buf);
  978.     if (out_of_mem) {
  979.         p_end = i-1;
  980.         return FALSE;
  981.     }
  982.     p_char[i] = '=';
  983.     for (i++; i<=p_end; i++) {
  984.         ret = pgets(buf, sizeof buf, pfp);
  985.         p_input_line++;
  986.         if (ret == Nullch)
  987.         fatal2("Unexpected end of file in patch at line %ld.\n",
  988.             p_input_line);
  989.         if (*buf != '>')
  990.         fatal2("> expected at line %ld of patch.\n", p_input_line);
  991.         p_line[i] = savestr(buf+2);
  992.         if (out_of_mem) {
  993.         p_end = i-1;
  994.         return FALSE;
  995.         }
  996.         p_len[i] = strlen(p_line[i]);
  997.         p_char[i] = '+';
  998.     }
  999.     }
  1000.     if (reverse)                        /* backwards patch? */
  1001.     if (!pch_swap())
  1002.         say1("Not enough memory to swap next hunk!\n");
  1003. #ifdef DEBUGGING
  1004.     if (debug & 2) {
  1005.     int i;
  1006.     char special;
  1007.  
  1008.     for (i=0; i <= p_end; i++) {
  1009.         if (i == p_ptrn_lines)
  1010.         special = '^';
  1011.         else
  1012.         special = ' ';
  1013.         fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);
  1014.         Fflush(stderr);
  1015.     }
  1016.     }
  1017. #endif
  1018.     if (p_end+1 < hunkmax)      /* paranoia reigns supreme... */
  1019.     p_char[p_end+1] = '^';  /* add a stopper for apply_hunk */
  1020.     return TRUE;
  1021. }
  1022.  
  1023. /* Input a line from the patch file, worrying about indentation. */
  1024.  
  1025. char *
  1026. pgets(bf,sz,fp)
  1027. char *bf;
  1028. int sz;
  1029. FILE *fp;
  1030. {
  1031.     char *ret = fgets(bf, sz, fp);
  1032.     Reg1 char *s;
  1033.     Reg2 int indent = 0;
  1034.  
  1035.     if (p_indent && ret != Nullch) {
  1036.     for (s=buf;
  1037.       indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) {
  1038.         if (*s == '\t')
  1039.         indent += 8 - (indent % 7);
  1040.         else
  1041.         indent++;
  1042.     }
  1043.     if (buf != s)
  1044.         Strcpy(buf, s);
  1045.     }
  1046.     return ret;
  1047. }
  1048.  
  1049. /* Reverse the old and new portions of the current hunk. */
  1050.  
  1051. bool
  1052. pch_swap()
  1053. {
  1054.     char **tp_line;        /* the text of the hunk */
  1055.     short *tp_len;        /* length of each line */
  1056.     char *tp_char;        /* +, -, and ! */
  1057.     Reg1 LINENUM i;
  1058.     Reg2 LINENUM n;
  1059.     bool blankline = FALSE;
  1060.     Reg3 char *s;
  1061.  
  1062.     i = p_first;
  1063.     p_first = p_newfirst;
  1064.     p_newfirst = i;
  1065.  
  1066.     /* make a scratch copy */
  1067.  
  1068.     tp_line = p_line;
  1069.     tp_len = p_len;
  1070.     tp_char = p_char;
  1071.     p_line = Null(char**);      /* force set_hunkmax to allocate again */
  1072.     p_len = Null(short*);
  1073.     p_char = Nullch;
  1074.     set_hunkmax();
  1075.     if (p_line == Null(char**) || p_len == Null(short*) || p_char == Nullch) {
  1076. #ifndef lint
  1077.     if (p_line == Null(char**))
  1078.         free((char*)p_line);
  1079.     p_line = tp_line;
  1080.     if (p_len == Null(short*))
  1081.         free((char*)p_len);
  1082.     p_len = tp_len;
  1083. #endif
  1084.     if (p_char == Nullch)
  1085.         free((char*)p_char);
  1086.     p_char = tp_char;
  1087.     return FALSE;        /* not enough memory to swap hunk! */
  1088.     }
  1089.  
  1090.     /* now turn the new into the old */
  1091.  
  1092.     i = p_ptrn_lines + 1;
  1093.     if (tp_char[i] == '\n') {           /* account for possible blank line */
  1094.     blankline = TRUE;
  1095.     i++;
  1096.     }
  1097.     if (p_efake >= 0) {                 /* fix non-freeable ptr range */
  1098.     if (p_efake <= i)
  1099.         n = p_end - i + 1;
  1100.     else
  1101.         n = -i;
  1102.     p_efake += n;
  1103.     p_bfake += n;
  1104.     }
  1105.     for (n=0; i <= p_end; i++,n++) {
  1106.     p_line[n] = tp_line[i];
  1107.     p_char[n] = tp_char[i];
  1108.     if (p_char[n] == '+')
  1109.         p_char[n] = '-';
  1110.     p_len[n] = tp_len[i];
  1111.     }
  1112.     if (blankline) {
  1113.     i = p_ptrn_lines + 1;
  1114.     p_line[n] = tp_line[i];
  1115.     p_char[n] = tp_char[i];
  1116.     p_len[n] = tp_len[i];
  1117.     n++;
  1118.     }
  1119.     assert(p_char[0] == '=');
  1120.     p_char[0] = '*';
  1121.     for (s=p_line[0]; *s; s++)
  1122.     if (*s == '-')
  1123.         *s = '*';
  1124.  
  1125.     /* now turn the old into the new */
  1126.  
  1127.     assert(tp_char[0] == '*');
  1128.     tp_char[0] = '=';
  1129.     for (s=tp_line[0]; *s; s++)
  1130.     if (*s == '*')
  1131.         *s = '-';
  1132.     for (i=0; n <= p_end; i++,n++) {
  1133.     p_line[n] = tp_line[i];
  1134.     p_char[n] = tp_char[i];
  1135.     if (p_char[n] == '-')
  1136.         p_char[n] = '+';
  1137.     p_len[n] = tp_len[i];
  1138.     }
  1139.     assert(i == p_ptrn_lines + 1);
  1140.     i = p_ptrn_lines;
  1141.     p_ptrn_lines = p_repl_lines;
  1142.     p_repl_lines = i;
  1143. #ifndef lint
  1144.     if (tp_line == Null(char**))
  1145.     free((char*)tp_line);
  1146.     if (tp_len == Null(short*))
  1147.     free((char*)tp_len);
  1148. #endif
  1149.     if (tp_char == Nullch)
  1150.     free((char*)tp_char);
  1151.     return TRUE;
  1152. }
  1153.  
  1154. /* Return the specified line position in the old file of the old context. */
  1155.  
  1156. LINENUM
  1157. pch_first()
  1158. {
  1159.     return p_first;
  1160. }
  1161.  
  1162. /* Return the number of lines of old context. */
  1163.  
  1164. LINENUM
  1165. pch_ptrn_lines()
  1166. {
  1167.     return p_ptrn_lines;
  1168. }
  1169.  
  1170. /* Return the probable line position in the new file of the first line. */
  1171.  
  1172. LINENUM
  1173. pch_newfirst()
  1174. {
  1175.     return p_newfirst;
  1176. }
  1177.  
  1178. /* Return the number of lines in the replacement text including context. */
  1179.  
  1180. LINENUM
  1181. pch_repl_lines()
  1182. {
  1183.     return p_repl_lines;
  1184. }
  1185.  
  1186. /* Return the number of lines in the whole hunk. */
  1187.  
  1188. LINENUM
  1189. pch_end()
  1190. {
  1191.     return p_end;
  1192. }
  1193.  
  1194. /* Return the number of context lines before the first changed line. */
  1195.  
  1196. LINENUM
  1197. pch_context()
  1198. {
  1199.     return p_context;
  1200. }
  1201.  
  1202. /* Return the length of a particular patch line. */
  1203.  
  1204. short
  1205. pch_line_len(line)
  1206. LINENUM line;
  1207. {
  1208.     return p_len[line];
  1209. }
  1210.  
  1211. /* Return the control character (+, -, *, !, etc) for a patch line. */
  1212.  
  1213. char
  1214. pch_char(line)
  1215. LINENUM line;
  1216. {
  1217.     return p_char[line];
  1218. }
  1219.  
  1220. /* Return a pointer to a particular patch line. */
  1221.  
  1222. char *
  1223. pfetch(line)
  1224. LINENUM line;
  1225. {
  1226.     return p_line[line];
  1227. }
  1228.  
  1229. /* Return where in the patch file this hunk began, for error messages. */
  1230.  
  1231. LINENUM
  1232. pch_hunk_beg()
  1233. {
  1234.     return p_hunk_beg;
  1235. }
  1236.  
  1237. /* Apply an ed script by feeding ed itself. */
  1238.  
  1239. void
  1240. do_ed_script()
  1241. {
  1242.     Reg1 char *t;
  1243.     Reg2 long beginning_of_this_line;
  1244.     Reg3 bool this_line_is_command = FALSE;
  1245.     Reg4 FILE *pipefp;
  1246.     FILE *popen();
  1247.  
  1248.     if (!skip_rest_of_patch) {
  1249.     Unlink(TMPOUTNAME);
  1250.     copy_file(filearg[0], TMPOUTNAME);
  1251.     if (verbose)
  1252.         Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
  1253.     else
  1254.         Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
  1255.     pipefp = popen(buf, "w");
  1256.     }
  1257.     for (;;) {
  1258.     beginning_of_this_line = ftell(pfp);
  1259.     if (pgets(buf, sizeof buf, pfp) == Nullch) {
  1260.         next_intuit_at(beginning_of_this_line,p_input_line);
  1261.         break;
  1262.     }
  1263.     p_input_line++;
  1264.     for (t=buf; isdigit(*t) || *t == ','; t++) ;
  1265.     this_line_is_command = (isdigit(*buf) &&
  1266.       (*t == 'd' || *t == 'c' || *t == 'a') );
  1267.     if (this_line_is_command) {
  1268.         if (!skip_rest_of_patch)
  1269.         fputs(buf, pipefp);
  1270.         if (*t != 'd') {
  1271.         while (pgets(buf, sizeof buf, pfp) != Nullch) {
  1272.             p_input_line++;
  1273.             if (!skip_rest_of_patch)
  1274.             fputs(buf, pipefp);
  1275.             if (strEQ(buf, ".\n"))
  1276.             break;
  1277.         }
  1278.         }
  1279.     }
  1280.     else {
  1281.         next_intuit_at(beginning_of_this_line,p_input_line);
  1282.         break;
  1283.     }
  1284.     }
  1285.     if (skip_rest_of_patch)
  1286.     return;
  1287.     fprintf(pipefp, "w\n");
  1288.     fprintf(pipefp, "q\n");
  1289.     Fflush(pipefp);
  1290.     Pclose(pipefp);
  1291.     ignore_signals();
  1292.     if (move_file(TMPOUTNAME, outname) < 0) {
  1293.     toutkeep = TRUE;
  1294.     chmod(TMPOUTNAME, filemode);
  1295.     }
  1296.     else
  1297.     chmod(outname, filemode);
  1298.     set_signals(1);
  1299. }
  1300.